<?php
/**
 * Created by PhpStorm.
 * User: ycp
 * Date: 2019/5/23
 * Time: 22:23
 */

namespace app\api\controller;



use app\common\func\Func;
use library\Controller;
use think\Exception;

class ApiCommon extends Controller
{
    protected $host;
    protected $openid;

    public function __construct()
    {
        parent::__construct();

//               $host = $_SERVER['SERVER_NAME'];
//        $host = $_SERVER['SERVER_ADDR'];
//        $host = '47.92.91.243';
        $host = 'www.huanqiupaozhe.cn';
        $this->host  = 'https://' . $host .'/';
        if(!empty($this->request->header('token'))){
            $token = $this->request->header('token');
            try{
                $this->openid = Func::jwtDecodeToken($token)->data->openid;
            }catch (\Exception $exception){
                if('Expired token' == $exception->getMessage()){
                    $this->error($exception->getMessage(), [], -99);
                }
            }
        }
//        $this->openid = 'omb7I5cSjfG1SAh372Uoc7rFFDKg';

//        $t = rand(1,2);
//        if($t = 1){
//            $this->error('', [], -99);
//        }
    }


    //获取图片路路径
    protected function getImageDir($pic){
        $host = $this->host;
        return $host . substr($pic, strpos($pic, 'upload/'));
    }

    public function downLoadFile()
    {
        // 响应头设置 我们就是通过设置header来跨域的 这就主要代码了 定义行为只是为了前台每次请求都能走这段代码
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:*');
        header('Access-Control-Allow-Headers:*');
        header('Access-Control-Allow-Credentials:false');
        $file = $this->request->param('name');
        return download(ROOT_PATH . '/static/common_file/' . $file);
    }

}